package xyz.kongbai121.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.kongbai121.demo.domain.Opinion;
import xyz.kongbai121.demo.domain.Tree;
import xyz.kongbai121.demo.mapper.*;
import xyz.kongbai121.demo.service.IOpinionService;
import xyz.kongbai121.demo.util.OpinionUtil;
import xyz.kongbai121.demo.util.UserPowerException;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Service
@Transactional
public class OpinionServiceImpl implements IOpinionService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private OpinionMapper opinionMapper;
    @Autowired
    private TreeMapper treeMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public String save(String content, Long treeId, Long opinionId, Long upOpinionId, String token) {
        String openid = redisTemplate.opsForValue().get(token);
        Integer state = userMapper.getState(openid);
        Tree tree = treeMapper.suGet(treeId);
        Opinion oldOpinion = null;
        //最后存储对象
        Opinion opinion = new Opinion();
        //TODO 如果有父评论
        if (opinionId != null){
            oldOpinion = opinionMapper.selectById(upOpinionId);
            if (opinionMapper.hasOpinion(opinionId) <= 0 || oldOpinion == null){
                throw new UserPowerException("非法操作");
            }
            //TODO 如果父评论不是自己,用户未读评论加一
            if(!oldOpinion.getOpenid().equals(openid)) userMapper.addUnreadComment(oldOpinion.getOpenid());
            //TODO 上级评论用户id
            opinion.setToOpenid(oldOpinion.getOpenid());
        }
        //判断是否有操作权限和文章是否存在
        if (state == 1 && tree != null) {
            //TODO 添加评论
            opinion.setContent(content);
            opinion.setOpenid(openid);
            opinion.setTreeId(treeId);
            opinion.setOpinionId(opinionId);
            opinion.setTreeBody(tree.getBody());
            opinion.setDeliverTime(new Date());
            opinion.setAuthorOpenid(tree.getOpenid());
            opinion.setIsRead(0);
            opinionMapper.insert(opinion);
            //TODO 如果不是自己评论自己,用户未读评论加一
            if(!tree.getOpenid().equals(openid)) userMapper.addUnreadOpinion(tree.getOpenid());
            //TODO 文章评论加1
            redisTemplate.boundValueOps("treeOpinionNum"+treeId).increment(1);
            return "评论成功";
        }else {
            throw new UserPowerException("没有权限");
        }
    }

    @Override
    public String delete(Long id, String token) {
        String openid = redisTemplate.opsForValue().get(token);
        Opinion opinion = opinionMapper.selectById(id);
        //判断评论是否是当前用户发的或者用户是管理员
        if (opinion.getOpenid() != null && opinion.getOpenid().equals(openid) || userMapper.getAdmin(openid) == 1){
            //TODO 删除评论
            opinionMapper.deleteById(id);
            //TODO 删除子评论
            UpdateWrapper<Opinion> wrapper = new UpdateWrapper<>();
            wrapper.eq("opinion_id",id);
            opinionMapper.delete(wrapper);
            //TODO 文章评论减1
            redisTemplate.boundValueOps("treeOpinionNum"+opinion.getTreeId()).increment(-1);
            //TODO 如果评论未读,未读数减1
            if (opinion.getIsRead() == 0) userMapper.subUnreadOpinion(opinion.getAuthorOpenid());
            return "操作成功";
        }else {
            throw new UserPowerException("没有权限");
        }
    }

    @Override
    public List<Opinion> list(Long treeId) {
        //TODO  查询文章所有的评论
        List<Opinion> list = opinionMapper.list(treeId);
        //TODO  处理父子评论关系
        CopyOnWriteArrayList<Opinion> opinions = new CopyOnWriteArrayList<>(list);
        return OpinionUtil.commentLink(opinions);
    }
}
